package edu.scau.misp.backend_module.service.impl;

import edu.scau.misp.backend_module.core.rms_dto.ProductDTO;
import edu.scau.misp.backend_module.domain.Addition;
import edu.scau.misp.backend_module.domain.Dish;
import edu.scau.misp.backend_module.domain.DishAddition;
import edu.scau.misp.backend_module.mapper.AdditionMapper;
import edu.scau.misp.backend_module.mapper.DishAdditionMapper;
import edu.scau.misp.backend_module.mapper.DishMapper;
import edu.scau.misp.backend_module.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private DishAdditionMapper dishAdditionMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private AdditionMapper additionMapper;



    /**
     * @param productSn
     * @return
     */
    @Override
    public ProductDTO getProductBySn(Long productSn) {

        DishAddition param = dishAdditionMapper.getDishAdditionBySn(productSn);
        ProductDTO product = new ProductDTO(param);
        return product;
    }

    /**
     * @return
     */
    @Override
    public List<ProductDTO> listAllProduct() {

        List<DishAddition> dishAdditionList = dishAdditionMapper.listAllDishAddition();
        List<ProductDTO> list = new ArrayList<>();
        for (DishAddition dishAddition : dishAdditionList) {
            // 使用提取的数据创建 ProductDTO 对象
            ProductDTO productDTO = new ProductDTO(dishAddition);
            // 将 ProductDTO 对象添加到 list 中
            list.add(productDTO);
        }
        return list;
    }

    /**
     * @param product
     * @return
     */
    @Override
    public List<ProductDTO> selectProductList(ProductDTO product) {


        Dish dish = new Dish();
        dish.setDishName(product.getProductName());
        Addition addition = new Addition();
        addition.setAdditionDescription(product.getFlavor());
        DishAddition param = new DishAddition();
//        Long test_1 = dishMapper.selectDishList(dish).get(0).getDishSn();
//        Long test_2 = additionMapper.selectAdditionList(addition).get(0).getAdditionSn();
//        param.setDishAdditionId(dishAdditionMapper.getDishAdditionBySn(param.getDishAdditionSn()).getDishAdditionId());
        param.setDishAdditionSn(product.getProductSn());
        param.setComboPrice(product.getProductPrice());
//        param.setComboDescription(product.getComboDescription());
//        模糊查询中取得第一个，十分不推荐;但是现在没有根据名称查数据的mapper------------------
        if (product.getFlavor()!=null){
            param.setAdditionSn(additionMapper.selectAdditionList(addition).get(0).getAdditionSn());
        }
        if(product.getProductName()!=null){
            param.setDishSn(dishMapper.selectDishList(dish).get(0).getDishSn());
        }
//        --------------------------------------------------------------------------------------
        List<DishAddition> dishAdditionList = dishAdditionMapper.selectDishAdditionList(param);
        List<ProductDTO> list = new ArrayList<>();
        for (DishAddition dishAddition : dishAdditionList) {
            // 使用提取的数据创建 ProductDTO 对象
            ProductDTO productDTO = new ProductDTO(dishAddition);
            // 将 ProductDTO 对象添加到 list 中
            list.add(productDTO);
        }
//        private Long dishAdditionId;
//        private Long dishAdditionSn;
//
//        private Long dishSn;
//        private Long additionSn;
//        private Long comboPrice;
//        private String comboDescription;





        return list;
    }

    /**
     * @param product
     * @return
     */
    @Override
    public int insertProduct(ProductDTO product) {

        Dish dish = new Dish();
        dish.setDishName(product.getProductName());
        Addition addition = new Addition();
        addition.setAdditionDescription(product.getFlavor());
        DishAddition param = new DishAddition();

        param.setComboPrice(product.getProductPrice());
//        模糊查询中取得第一个，十分不推荐;但是现在没有根据名称查数据的mapper------------------
        if (product.getFlavor()!=null){
            param.setAdditionSn(additionMapper.selectAdditionList(addition).get(0).getAdditionSn());
        }
        if(product.getProductName()!=null){
            param.setDishSn(dishMapper.selectDishList(dish).get(0).getDishSn());
        }


        Long DS = dishMapper.selectDishList(dish).get(0).getDishSn();
        Long AS = additionMapper.selectAdditionList(addition).get(0).getAdditionSn();

        // 将Long转换为String
        String dsString = String.valueOf(DS);
        String asString = String.valueOf(AS);

// 拼接字符串
        String combinedString = dsString + asString;
// 尝试将拼接后的字符串转换回Long
        try {
            Long combinedNumber = Long.parseLong(combinedString);
            param.setDishAdditionSn(combinedNumber); // 假设这个方法接受一个Long参数
        } catch (NumberFormatException e) {
            // 处理转换异常，例如打印错误信息或设置默认值等
            e.printStackTrace();

        }
//        param.setDishAdditionSn();


        return dishAdditionMapper.insertDishAddition(param);
    }

    /**
     * @param product
     * @return
     */
    @Override
    public int updateProduct(ProductDTO product) {

        Dish dish = new Dish();
        dish.setDishName(product.getProductName());
        Addition addition = new Addition();
        addition.setAdditionDescription(product.getFlavor());
        DishAddition param = new DishAddition();

        param.setComboPrice(product.getProductPrice());
//        模糊查询中取得第一个，十分不推荐;但是现在没有根据名称查数据的mapper------------------
        if (product.getFlavor()!=null){
            param.setAdditionSn(additionMapper.selectAdditionList(addition).get(0).getAdditionSn());
        }
        if(product.getProductName()!=null){
            param.setDishSn(dishMapper.selectDishList(dish).get(0).getDishSn());
        }


        Long DS = dishMapper.selectDishList(dish).get(0).getDishSn();
        Long AS = additionMapper.selectAdditionList(addition).get(0).getAdditionSn();

        // 将Long转换为String
        String dsString = String.valueOf(DS);
        String asString = String.valueOf(AS);

// 拼接字符串
        String combinedString = dsString + asString;
// 尝试将拼接后的字符串转换回Long
        try {
            Long combinedNumber = Long.parseLong(combinedString);
            param.setDishAdditionSn(combinedNumber); // 假设这个方法接受一个Long参数
        } catch (NumberFormatException e) {
            // 处理转换异常，例如打印错误信息或设置默认值等
            e.printStackTrace();

        }
        return dishAdditionMapper.updateDishAddition(param);
    }

    /**
     * @param productSn
     * @return
     */
    @Override
    public int deleteProductBySn(Long productSn) {


        return dishAdditionMapper.deleteDishAdditionBySn(productSn);
    }
}
